knitr::opts_chunk$set(echo = TRUE)
#headers for SILO file
headers <- c("Date", "Day", "Date2", "T.Max", "Smx", "T.Min", "Smn", "Rain",
             "Srn", "Evap", "Sev", "Radn", "Ssl", "VP", "Svp", "RHmaxT",
             "RHminT", "FAO56")


#get site details from the csv file

spatial <- readr::read_csv("BoM_ETA_20150501-20160430/spatial/StationData.csv")
spatial <- spatial[spatial$REGION == "QLD", ]

head(spatial)

testdf <- data.frame(site = NA, run = NA)

# check access to each site

tryCatch(for (station_number in spatial$station_number) {
  site_number <- as.character(station_number)
  URL <- paste0("https://www.longpaddock.qld.gov.au/cgi-bin/silo/PatchedPointDataset.php?format=fao56&station=", site_number, "&start=20160101&finish=20160102&username=USQKEITH&password=KEITH4350")
  site_data <- read.table(URL, skip = 16)
  testdf <- rbind(testdf, c(station_number, site_data[1, 1]))
}, error = function(x) cat(paste0("\nYou've reached the end of stations available in this data set. The testdf data frame is complete.")))

testdf <- subset(testdf, run == 15)

# Select only stations that are available from SILO
spatial <- spatial[spatial$station_number %in% testdf$site, ]
spatial <- subset(spatial, station_number != 29039 & station_number != 40284 & station_number != 40651 & station_number != 41097 & station_number != 45009)

number_of_sites <- length(spatial$station_number)
number_of_sites

QLD_SILO_and_hourly_stations <- spatial

devtools::use_data(QLD_SILO_and_hourly_stations, overwrite = TRUE, compress = "bzip2")
# establish the dates to be working within
# enter how many days to look back

dates <- c("2015-04-30", "2016-04-29")
dates <- gsub("-", "", dates)

# creates the data cube
SILO_array <- array(data = NA, dim = c(366, 18, number_of_sites),
                  dimnames = list(NULL, headers, spatial$station_number))

for (i in spatial$station_number) {
  site_number <- as.character(i)
  URL <- paste0("https://www.longpaddock.qld.gov.au/cgi-bin/silo/PatchedPointDataset.php?format=fao56&station=", i, "&start=", dates[1], "&finish=", dates[2], "&username=USQKEITH&password=KEITH4350")
  site_data <- read.table(URL, skip = 41)
  colnames(site_data) <- headers
  site_data <- data.matrix(site_data) 
  SILO_array[,, site_number] <- site_data 
}
devtools::use_data(SILO_array, overwrite = TRUE, compress = "bzip2")
#libarys required for mapping
library("raster")
library("ggplot2")
library("rgeos")
library("maptools")
library("rgdal")

#plot qld outline and the surface over the top of surface
Oz <- getData(name = "GADM", download = TRUE, country = "AUS", level = 1)

# Subset Australia data to QLD
QLD <- Oz[Oz@data$NAME_1 == "Queensland", ]

QLD <- fortify(QLD, id = "ID_1")

ggplot(QLD, aes(x = long, y = lat)) +
  geom_polygon(fill = "#cccccc") +
  geom_point(data = spatial, aes(x = "LONGITUDE", y = "LATITUDE")) +
  coord_map()


ToowoombaTrio/John_Conner documentation built on May 9, 2019, 5:11 p.m.